package com.liuche.pictureserver.utils;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 文件相关工具类
 *
 * @author liuche
 */
public class FileUtil {
    // 第二种方式
    public static MultipartFile toMultipartFile(File file, String contentType) throws IOException {
        FileInputStream input = new FileInputStream(file);
        String name = file.getName();
        String type = contentType.substring(contentType.lastIndexOf("/") + 1);
        if (name.lastIndexOf(".") > 0) {
            name = name.substring(0, name.lastIndexOf("."));
        }
        MockMultipartFile multipartFile = new MockMultipartFile(
                "file",
                name + "." + type,
                contentType,
                input
        );

        input.close();
        return multipartFile;
    }


}
